package com.gxa.saas.portal.service.impl;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.gxa.saas.portal.service.ClassifyService;
import io.searchbox.client.JestClient;
import io.searchbox.core.Search;
import io.searchbox.core.SearchResult;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: pengxtao
 * @Date: 2022/7/23下午4:42
 */
@Service
public class ClassifyServiceImpl implements ClassifyService {
    @Autowired
    private JestClient jestClient;

    private static final String INDEX="goods_table3";

    private static final String TYPE="_doc";

    private static final String FIRST_NAME="firstName";

    @Override
    public Map ClaSyselect(String firstName,Integer pageNumber, Integer pageSize) {
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        //全文搜索
        QueryBuilder queryBuilder = QueryBuilders.termQuery(FIRST_NAME, firstName);//单值完全匹配查询
        sourceBuilder.query(queryBuilder);
        sourceBuilder.from((pageNumber - 1) * pageSize);//设置起始页
        sourceBuilder.size(pageSize);//设置页大小

        Search search = new Search.Builder(sourceBuilder.toString()).addIndex(INDEX).addType(TYPE).build();

        try {
            SearchResult jestResult = jestClient.execute(search);
            JsonObject jsonObject = jestResult.getJsonObject();
            JsonObject hitsobject = jsonObject.getAsJsonObject("hits");
            Map jsonMap = jestResult.getJsonMap();
            Map hits = (Map) jsonMap.get("hits");
            List<Map<String,Object>> hits1 = (List) hits.get("hits");
            List list = new ArrayList();
            for (int i=0;i<hits1.size();i++) {
                Object source = hits1.get(i).get("_source");
                list.add(source);
            }

            /*总条数*/
            JsonObject total = hitsobject.getAsJsonObject("total");
            JsonElement value = total.get("value");
            Integer value2 = Integer.parseInt(String.valueOf(value));
            //System.out.println(value+"总条数");
            /*总页数*/
            Integer pageTotal = value2/pageSize+1;
            Map map  = new HashMap();
            map.put("code",200);
            map.put("data",list);
            map.put("pageTotal",pageTotal);
            map.put("total",value2);

            return map;
        } catch (Exception e) {
            e.printStackTrace();
            Map map  = new HashMap();
            map.put("code",500);
            map.put("message","系统异常");
            return map;
        }
    }

    @Override
    public Map ClaMoneyselect(String firstName, Integer values,Integer pageNumber, Integer pageSize) {
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        //全文搜索
        QueryBuilder queryBuilder = QueryBuilders.termQuery(FIRST_NAME, firstName);//单值完全匹配查询
        sourceBuilder.query(queryBuilder);
        //字段过滤
        sourceBuilder.fetchSource(new String[]{"prId","prName","prPrice","picture","firstName"},null);
        //排序
        sourceBuilder.from((pageNumber - 1) * pageSize);//设置起始页
        sourceBuilder.size(pageSize);//设置页大小
        if (values==1){
            sourceBuilder.sort("prPrice", SortOrder.DESC);
        }
        if (values==0){
            sourceBuilder.sort("prPrice", SortOrder.ASC);
        }
        Search search = new Search.Builder(sourceBuilder.toString()).addIndex(INDEX).addType(TYPE).build();

        try {
            SearchResult jestResult = jestClient.execute(search);
            JsonObject jsonObject = jestResult.getJsonObject();
            JsonObject hitsobject = jsonObject.getAsJsonObject("hits");
            Map jsonMap = jestResult.getJsonMap();
            Map hits = (Map) jsonMap.get("hits");
            List<Map<String,Object>> hits1 = (List) hits.get("hits");
            List list = new ArrayList();
            for (int i=0;i<hits1.size();i++) {
                Object source = hits1.get(i).get("_source");
                list.add(source);
            }
            /*总条数*/
            JsonObject total = hitsobject.getAsJsonObject("total");
            JsonElement value = total.get("value");
            Integer value2 = Integer.parseInt(String.valueOf(value));
            //System.out.println(value+"总条数");
            /*总页数*/
            Integer pageTotal = value2/pageSize+1;
            Map map  = new HashMap();
            map.put("code",200);
            map.put("data",list);
            map.put("pageTotal",pageTotal);
            map.put("total",value2);

            return map;
        } catch (Exception e) {
            e.printStackTrace();
            Map map  = new HashMap();
            map.put("code",500);
            map.put("message","系统异常");
            return map;
        }
    }

    @Override
    public Map ClaSalesSelect(String firstName, Integer values,Integer pageNumber, Integer pageSize) {
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        //全文搜索
        QueryBuilder queryBuilder = QueryBuilders.termQuery(FIRST_NAME, firstName);//单值完全匹配查询
        sourceBuilder.query(queryBuilder);
        //字段过滤
        sourceBuilder.fetchSource(new String[]{"prId","prName","prPrice","picture","firstName"},null);
        //排序
        sourceBuilder.from((pageNumber - 1) * pageSize);//设置起始页
        sourceBuilder.size(pageSize);//设置页大小
        if (values==1){
            sourceBuilder.sort("sales", SortOrder.DESC);
        }
        if (values==0){
            sourceBuilder.sort("sales", SortOrder.ASC);
        }
        Search search = new Search.Builder(sourceBuilder.toString()).addIndex(INDEX).addType(TYPE).build();

        try {
            SearchResult jestResult = jestClient.execute(search);
            JsonObject jsonObject = jestResult.getJsonObject();
            JsonObject hitsobject = jsonObject.getAsJsonObject("hits");
            Map jsonMap = jestResult.getJsonMap();
            Map hits = (Map) jsonMap.get("hits");
            List<Map<String,Object>> hits1 = (List) hits.get("hits");
            List list = new ArrayList();
            for (int i=0;i<hits1.size();i++) {
                Object source = hits1.get(i).get("_source");
                list.add(source);
            }
            /*总条数*/
            JsonObject total = hitsobject.getAsJsonObject("total");
            JsonElement value = total.get("value");
            Integer value2 = Integer.parseInt(String.valueOf(value));
            //System.out.println(value+"总条数");
            /*总页数*/
            Integer pageTotal = value2/pageSize+1;
            Map map  = new HashMap();
            map.put("code",200);
            map.put("data",list);
            map.put("pageTotal",pageTotal);
            map.put("total",value2);

            return map;
        } catch (Exception e) {
            e.printStackTrace();
            Map map  = new HashMap();
            map.put("code",500);
            map.put("message","系统异常");
            return map;
        }
    }
}
